<?php
/**
 * 关系类型
 */
declare(strict_types=1);
namespace app\core;

use think\facade\Db;

class Relation
{
    /**
     * 获取关系模型
     */
    public static function getModel($tName='',$type=0)
    {
        $model=Db::table($tName);
        return $model;
    }

    public static function createTable($table,$cn){
        $sql="
        CREATE TABLE `$table` (
          `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
          `weight` int(11) DEFAULT '0' COMMENT '权重',
          `status` tinyint(1) DEFAULT '0' COMMENT '状态',
          `cdate` datetime DEFAULT NULL COMMENT '创建时间',
          `is_deled` tinyint(1) DEFAULT '0' COMMENT '是否删除',
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='$cn'";
        Db::execute($sql);
    }

    public static function addTableField($tableId,$fieldname,$cnname,$type_id){
        $res=false;
        $data=Res::findTableData('table_list',['id'=>$tableId],'table_name');
        if($data){
            $tableName=$data['table_name'];
        }else{
            return $res;
        }
        if($type_id>0 && $type_id<13){
            $typename=Res::getConfig('cfg.fieldtype',$type_id);
        }else{
            return $res;
        }

        if($type_id>0 && $type_id<6){
            $sql="
             ALTER TABLE  `$tableName` ADD  `$fieldname` $typename DEFAULT '0' COMMENT '$cnname'";

        }elseif($type_id>5 && $type_id<13){
            $sql="
             ALTER TABLE  `$tableName` ADD  `$fieldname` $typename DEFAULT NULL COMMENT '$cnname'";
        }
        

        if($type_id==10){
            $sql="
        ALTER TABLE  `$tableName` ADD  `$fieldname` $typename  COMMENT '$cnname'";
        }

        Db::execute($sql);
        return true;
    }

    public static function addRelation($table,$maintable,$childtable,$cn){
        $sql="
        CREATE TABLE `$table` (
          `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
          `$maintable` int(11) NOT NULL COMMENT '$maintable',
          `$childtable` int(11) NOT NULL COMMENT '$childtable',
          `weight` int(11) DEFAULT '0' COMMENT '权重',
          `status` tinyint(1) DEFAULT '0' COMMENT '状态',
          `cdate` datetime DEFAULT NULL COMMENT '创建时间',
          `is_deled` tinyint(1) DEFAULT '0' COMMENT '是否删除',
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='$cn'";
        Db::execute($sql);
    }

    

}